package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class MBCSACRInputFormPeriod extends X_BCS_ACRInputForm_Period{

//	private Mbcsinputfinreportsline[] lines;
	private static CLogger s_log = CLogger.getCLogger(MBCSACRInputFormPeriod.class);
	
	
	public MBCSACRInputFormPeriod(Properties ctx, int MBCSACRInputFormPeriod_ID,
			String trxName) {
		super(ctx, MBCSACRInputFormPeriod_ID, trxName);
		// TODO Auto-generated constructor stub
	}
	
	public MBCSACRInputFormPeriod(Properties ctx, ResultSet rs, String trxName){
		super(ctx, rs, trxName);
	}

	protected boolean afterDelete(boolean success){
		
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";	

		//delete companies
		sql = " SELECT BCS_ACRInputForm_Company_ID FROM BCS_ACRInputForm_Company \n " +
				   " WHERE BCS_ACRInputForm_Period_ID ="  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					MBCSACRInputFormCompany company = new MBCSACRInputFormCompany(Env.getCtx(), rs.getInt(1), null);
					if(company != null)
						company.delete(true);
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormPeriod", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

		return true;
	}
	
	
	protected boolean afterSave(boolean newRecord, boolean success){
		
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";	
		List<Integer> list = new ArrayList<Integer>();

		sql = 	" select c_bpartner_id \n"	+
				"	from adempiere.BCS_CompPerimeterDict \n " +
				" 	order by c_bpartner_id	";

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					list.add(rs.getInt(1));
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormCompany", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

			for(Integer i: list){
				MBCSACRInputFormCompany company = new MBCSACRInputFormCompany(Env.getCtx(), 0, null);
				company.setC_BPartner_ID(i);
				company.setBCS_ACRInputForm_Period_ID(get_ID());
				company.setC_DocType_ID(1000075);
				company.setDocStatus("EM");
				company.setDocAction("LD");
				switch(i){
					case 1000084:
						company.setAD_Org_ID(1000000);
						break;
					case 1000022:
						company.setAD_Org_ID(1000001);
						break;
					case 1000021:
						company.setAD_Org_ID(1000002);
						break;
					case 1000018:
						company.setAD_Org_ID(1000003);
						break;
					case 1000020:
						company.setAD_Org_ID(1000006);
						break;
					case 1000019:
						company.setAD_Org_ID(1000004);
						break;
					case 2000086:
						company.setAD_Org_ID(1000005);
						break;
				}
				company.setIsLoaded(false);
				company.setIsChecked(false);
				company.setIsCommited(false);
				company.save();
				
				
			}
			
		
	return true;
}	

	
}
